<script>
export default {
  data() {
    return {
      inputValue: {
        inputArea: '',
        textArea: '',
      },
      error: {
        inputArea: '',
        textArea: '',
      },
      isSubmit: false,

    };
  },
  methods: {
    handleSubmit() {
      this.error.inputArea = this.inputValue.inputArea.length === 0 ? '昵称不能为空' : ''
      this.error.textArea = this.inputValue.textArea.length === 0 ? '内容不能为空' : ''
      if (this.error.inputArea || this.error.textArea) {
        return;
      }
      this.isSubmit = true
      this.$emit('submit', this.inputValue, (msg) => {
        this.$showMessage({
          content: msg,
          type: 'success',
          duration: 1000,
          container: this.$refs.form,
          callback: () => {
            this.isSubmit = false
            this.inputValue.inputArea = ''
            this.inputValue.textArea = ''
          }
        })
      })
    }
  },

};
</script>

<template>
  <form id="data-form-container" v-on:submit.prevent="handleSubmit" ref="form" class="data-form-container">
    <div class="form-item">
      <div class="input-area">
        <input type="text" maxlength="10" placeholder="用户昵称" v-model="inputValue.inputArea" />
        <span class="tip">{{ inputValue.inputArea.length }}/10</span>
      </div>
      <div class="error">{{ error.inputArea }}</div>
    </div>
    <div class="form-item">
      <div class="text-area">
        <textarea maxlength="300" placeholder="输入内容" v-model="inputValue.textArea"></textarea>
        <span class="tip">{{ inputValue.textArea.length }}/300</span>
      </div>
      <div class="error">{{ error.textArea }}</div>
    </div>
    <div class="form-item">
      <div class="button-area">
        <button :disabled="isSubmit">
          {{ isSubmit ? '提交中...' : '提交' }}
        </button>
      </div>
    </div>
  </form>
</template>



<style scoped lang="less">
@import "~@/styles/var.less";

.data-form-container {
  padding: 0 10px;
  margin-bottom: 20px;
  overflow: hidden;
}

.form-item {
  margin-bottom: 8px;
}

.input-area {
  width: 50%;
  position: relative;
}

.text-area {
  position: relative;
}

.tip {
  position: absolute;
  right: 5px;
  bottom: 5px;
  color: #b4b8bc;
  font-size: 12px;
}

input,
textarea {
  display: block;
  width: 100%;
  box-sizing: border-box;
  border: 1px dashed @gray;
  outline: none;
  color: @words;
  font-size: 14px;
  border-radius: 4px;

  &:focus {
    border-color: @primary;
  }
}

input {
  padding: 0 15px;
  height: 40px;
}

textarea {
  resize: none;
  padding: 8px 15px;
  height: 120px;
}

.error {
  margin-top: 6px;
  color: @danger;
  font-size: 14px;
  height: 20px;
  line-height: 20px;
}

button {
  position: relative;
  cursor: pointer;
  border: none;
  outline: none;
  width: 100px;
  height: 34px;
  color: #fff;
  border-radius: 4px;
  background: @primary;

  &:hover {
    background: darken(@primary, 10%);
  }

  &:disabled {
    background: lighten(@primary, 20%);
    cursor: not-allowed;
  }
}
</style>
